package com.weplay.gameplane;


import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;


import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.CSJAdError;
import com.bytedance.sdk.openadsdk.CSJSplashAd;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTSplashAd;
import com.weplay.gameplane.R;
import com.weplay.gameplane.holders.TTAdManagerHolder;
import com.weplay.gameplane.utils.ChuanShanJiaUtil;
import com.weplay.gameplane.utils.TToast;

public class CsjSplashActivity extends Activity {
    private static final String TAG = "SplashActivity";
    private TTAdNative mTTAdNative;
    private FrameLayout mSplashContainer;
    //是否强制跳转到主页面
    private boolean mForceGoMain;

    //开屏广告加载超时时间,建议大于3000,这里为了冷启动第一次加载到广告并且展示,示例设置了3000ms
    private static final int AD_TIME_OUT = 4000;
    private String mCodeId = "开屏广告位ID";
    private boolean mIsExpress = false; //是否请求模板广告
    private boolean mIsHalfSize = false;
    private static CsjSplashActivity _Instance;
    public static  CsjSplashActivity Inst(){
        return _Instance;
    }
    @SuppressWarnings("RedundantCast")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _Instance = this;
        setContentView(R.layout.activity_splash);
        mSplashContainer = (FrameLayout) findViewById(R.id.splash_container);
        //step2:创建TTAdNative对象
        mTTAdNative = TTAdManagerHolder.get().createAdNative(this);
        hideBottomUIMenu();  //隐藏虚拟按键，并且全屏
        getExtraInfo();

        TTAdManagerHolder.Inst().init(this,"5393240","GamePlane");
    }

    private void getExtraInfo() {
        Intent intent = getIntent();
        if(intent == null) {
            return;
        }
        String codeId = "888288840";
        if (!TextUtils.isEmpty(codeId)){
            mCodeId = codeId;
        }
        mIsExpress = intent.getBooleanExtra("is_express", false);
        mIsHalfSize = intent.getBooleanExtra("is_half_size", false);
    }

    @Override
    protected void onResume() {
        //判断是否该跳转到主页面
        if (mForceGoMain) {
            goToMainActivity();
        }
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mForceGoMain = true;
    }
    /**
     * 加载开屏广告
     */
    public void loadSplashAd(){
        AdSlot adSlot=null;
        float shWidthDp = ChuanShanJiaUtil.getScreenWidthDp(this);
        int splashWidthPx = ChuanShanJiaUtil.getScreenWidthInPx(this);
        int screenHeightPx = ChuanShanJiaUtil.getScreenHeight(this);
        float screenHeightDp = ChuanShanJiaUtil.px2dip(this, screenHeightPx);
        float splashHeightDp;
        int splashHeightPx;
        if (mIsHalfSize) {
            // 开屏高度 = 屏幕高度 - 下方预留的高度，demo中是预留了屏幕高度的1/5，因此开屏高度传入 屏幕高度*4/5
            splashHeightDp = screenHeightDp * 4/5.f;
            splashHeightPx = (int) (screenHeightPx * 4/5.f);
        } else {
            splashHeightDp = screenHeightDp;
            splashHeightPx = screenHeightPx;
        }
        if (mIsExpress) {
            //个性化模板广告需要传入期望广告view的宽、高，单位dp，请传入实际需要的大小，
            //比如：广告下方拼接logo、适配刘海屏等，需要考虑实际广告大小
//            float expressViewWidth = ChuanShanJiaUtil.getScreenWidthDp(this);
//            float expressViewHeight = ChuanShanJiaUtil.getHeight(this);
            adSlot = new AdSlot.Builder()
                    .setCodeId(mCodeId)
                    .setSupportDeepLink(true)
                    .setImageAcceptedSize(splashWidthPx, splashHeightPx)
                    //模板广告需要设置期望个性化模板广告的大小,单位dp,代码位是否属于个性化模板广告，请在穿山甲平台查看
//                    .setExpressViewAcceptedSize(splashWidthDp, splashHeightDp)
                    .build();
        } else {
            adSlot = new AdSlot.Builder()
                    .setCodeId(mCodeId)
                    .setSupportDeepLink(true)
                    .setImageAcceptedSize(splashWidthPx, splashHeightPx)
                    .build();
        }
        //step4:请求广告，调用开屏广告异步请求接口，对请求回调的广告作渲染处理
        mTTAdNative.loadSplashAd(adSlot, new TTAdNative.CSJSplashAdListener() {
            @Override
            public void onSplashLoadSuccess() {
                Log.d(TAG,"loadSplashAd success");
                Log.d(TAG, "开屏广告请求成功");

            }

            @Override
            public void onSplashLoadFail(CSJAdError csjAdError) {
                Log.d(TAG,"loadSplashAd OnError"+ csjAdError.getMsg() +" code:" );
                showToast(csjAdError.getMsg());
                goToMainActivity();
            }

            @Override
            public void onSplashRenderSuccess(CSJSplashAd csjSplashAd) {

            }

            @Override
            public void onSplashRenderFail(CSJSplashAd csjSplashAd, CSJAdError csjAdError) {

            }

        },AD_TIME_OUT);
    }

    /**
     * 跳转到主页面
     */
    private void goToMainActivity(){

    }

    private void showToast(String msg) {
        TToast.show(this, msg);
    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }
}